//
//  BQAddRessTableVC.m
//  我的收货地址
//
//  Created by 万伶童 on 16/11/20.
//  Copyright © 2016年 解延峰. All rights reserved.
//

#import "BQAddRessTableVC.h"

#import "BQGoodsAddressModel.h"
//static NSString * cellID = @"cell_id";
@interface BQAddRessTableVC ()<UIPickerViewDataSource,UIPickerViewDelegate>
/** 省 **/
@property (strong,nonatomic)NSArray *provinceList;
/** 市 **/
@property (strong,nonatomic)NSArray *cityList;
/** 区 **/
@property (strong,nonatomic)NSArray *areaList;
/** 第一级选中的下标 **/
@property (assign, nonatomic)NSInteger selectOneRow;
/** 第二级选中的下标 **/
@property (assign, nonatomic)NSInteger selectTwoRow;
/** 第三级选中的下标 **/
@property (assign, nonatomic)NSInteger selectThreeRow;


@end

@implementation BQAddRessTableVC
-(BQGoodsAddressModel *)Addmodel
{
    if (_Addmodel == nil)
    {
        _Addmodel = [[BQGoodsAddressModel alloc]init];
    }
    return _Addmodel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
   // MARK:接收传过来的数据
    _addRessV.nameText.text =  self.Addmodel.accept_name;
    _addRessV.phoneNumText.text = self.Addmodel.telphone;
    _addRessV.addRessText.text = self.Addmodel.address;
    _addRessV.cityText.text = self.Addmodel.city_name;
    _addRessV.areaText.text = self.Addmodel.district_name;
    _addRessV.tmpBtn.tag = self.Addmodel.gender.intValue;
    _addRessV.indexpTwo = _indexpOne;
    
    
   
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加地址";
    
    [self getCityListJSON];//获取数据
    [self getCitydate:0];// 默认显示数据
    [self getAreaDate:0];
    [self addPickView];
    
      self.tableView.contentInset = UIEdgeInsetsMake(80, 0, 0, 0);
}


    
    



- (void)setupUI
{
    BQAddRessView * addRessV = [[[UINib nibWithNibName:@"BQAddRess" bundle:nil]instantiateWithOwner:nil options:nil]lastObject];
    _addRessV = addRessV;
    
    
    self.tableView.tableHeaderView = addRessV;
    
    

    //MARK:清空下面的cell;
    self.tableView.tableFooterView = [[UIView alloc]init];
    //MARK:设置保存按钮
    UIBarButtonItem * seaveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(seave)];
    self.navigationItem.rightBarButtonItem = seaveBtn;
    
}


//MARK:保存监听
- (void)seave
{
    //保存赋值
   
    
    self.Addmodel.accept_name = _addRessV.nameText.text;
    self.Addmodel.telphone = _addRessV.phoneNumText.text;
    self.Addmodel.address = _addRessV.addRessText.text;
    self.Addmodel.city_name = _addRessV.cityText.text;
    self.Addmodel.district_name = _addRessV.areaText.text;
    
    
    
    if (_addRessV.tmpBtn.tag!=0)
    {
        self.Addmodel.gender = [NSString stringWithFormat:@"%zd",_addRessV.tmpBtn.tag];
        
    }
    if (!_addRessV.nameText.text || _addRessV.nameText.text.length == 0) {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入收货人姓名" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
            
            [alert show];
            
        }else if (_addRessV.phoneNumText.isEditing == NO){
           
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入鲜蜂侠联系您的电话" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
            
            [alert show];
        }else if (_addRessV.addRessText.isEditing == NO){
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选择城市" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
            
            [alert show];
        }else if (_addRessV.cityText.isEditing == NO){
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选择您的收货地址" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
            
            [alert show];
        }else if (_addRessV.areaText.isEditing == NO){
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入门牌号等详细信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil,nil];
            
            [alert show];
        }else{
            
          if ([self.delegate respondsToSelector:@selector(addRessTableVC:addModel:)])
                {
                    [self.delegate addRessTableVC:self addModel:_Addmodel];
                    [self.navigationController popViewControllerAnimated:YES];
                }   
        }
    

}

+ (BOOL) isMobile:(NSString *)mobileNumbel{
    
    
    
    
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    
    
    
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    
    return NO;
}


//MARK:===========
- (void)addPickView{
    
    UIPickerView *pickView = [[UIPickerView alloc] init];
    pickView.delegate = self;
    pickView.dataSource = self;
    pickView.backgroundColor = [UIColor colorWithRed:200 green:200 blue:200 alpha:0.5];
    self.addRessV.cityText.inputView = pickView;
}
/**
 *  读取城市文件
 */
- (void)getCityListJSON{
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"city" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error;
    NSArray *provinceList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    self.provinceList = provinceList;
    
}
- (void)getCitydate:(NSInteger)row{
    
    
    if ([self.provinceList[row][@"type"] intValue] == 0) {
        NSArray *cityArr = [[NSArray alloc] initWithObjects:self.provinceList[row], nil];
        self.cityList = cityArr;
        
    }else{
        NSMutableArray *cityList = [[NSMutableArray alloc] init];
        for (NSArray *cityArr in self.provinceList[row][@"sub"]) {
            [cityList addObject:cityArr];
        }
        self.cityList = cityList;
    }
    
    
}
- (void)getAreaDate:(NSInteger)row{
    if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) {
        NSMutableArray *areaList = [[NSMutableArray alloc] init];
        for (NSArray *cityDict in self.provinceList[self.selectOneRow][@"sub"]) {
            [areaList addObject:cityDict];
        }
        self.areaList = areaList;
    }else{
        
        NSMutableArray *areaList = [[NSMutableArray alloc] init];
        for (NSArray *cityDict in self.cityList[row][@"sub"]) {
            [areaList addObject:cityDict];
        }
        self.areaList = areaList;
    }
    
}
#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return self.provinceList.count;
    }else if (component == 1){
        return self.cityList.count;
    }else if (component == 2){
        return self.areaList.count;
    }
    return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    static NSInteger oneRow = 0;
    static NSInteger tweRow = 0;
    static NSInteger threeRow = 0;
    if (component == 0) {
        
        self.selectOneRow = row;
        [self getCitydate:row];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [self getAreaDate:0];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) {
            
            self.selectTwoRow = 0;
        }
        oneRow = row;
        tweRow = 0;
        threeRow = 0;
        
    }
    if (component == 1){
        
        self.selectTwoRow = row;
        [self getAreaDate:row];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        
        tweRow = row;
        threeRow = 0;
    }
    if (component == 2){
        
        self.selectThreeRow = row;
        threeRow = row;
    }
    NSMutableString *regionAddress = [[NSMutableString alloc] init];
    if (oneRow > 0 &&[self.provinceList[self.selectOneRow][@"type"] intValue] != 0 ) {
        [regionAddress appendFormat:@"%@省",self.provinceList[self.selectOneRow][@"name"]];
        
    }
    if (tweRow > 0 || [self.provinceList[self.selectOneRow][@"type"] intValue] == 0){
        
        [regionAddress appendFormat:@"%@市",self.cityList[self.selectTwoRow][@"name"]];
    }
    if (threeRow > 0 ){
        [regionAddress appendFormat:@"%@",self.areaList[self.selectThreeRow][@"name"]];
    }
    self.addRessV.cityText.text = regionAddress;
    // MARK: 选择的省 市 区域
    NSLog(@"%@",self.addRessV.cityText.text);
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        
        return self.provinceList[row][@"name"];
        
    }
    if (component == 1){
        if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) {
            
            
            return self.cityList[0][@"name"];
        }else {
            
            return self.cityList[row][@"name"];
        }
        
    }
    if (component == 2){
        
        return self.areaList[row][@"name"];
    }
    return nil;
}




@end
